﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditTravelGuideCash : AdminPageBase
    {
        private TravelGuideCashInfo _travelGuideCashInfo = new TravelGuideCashInfo();
        private readonly TravelGuideCashController _travelGuideCashController = new TravelGuideCashController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TravelGuideCashId"]))
                _travelGuideCashInfo = _travelGuideCashController.GetTravelGuideCash(int.Parse(Request.QueryString["TravelGuideCashId"]));

            if (Page.IsPostBack) return;

            LoadStatus();
            LoadAdminUser();
            LoadTravelGuide();
            LoadTime();

            if (_travelGuideCashInfo.TravelGuideCashId == -1) return;

            LoadTravelGuidePrice();

            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListTravelGuideCash.aspx?xml=TravelGuideCash");
        }

        private void SaveData()
        {
            _travelGuideCashInfo.TravelGuideId = int.Parse(ddlTravelGuide.SelectedValue);
            _travelGuideCashInfo.TravelGuidePriceId = int.Parse(Request["ctl00$placeHolderContent$ddlTravelGuidePrice"]);
            if (!String.IsNullOrEmpty(dteBeginDate.Value))
            {
                var beginDate = Convert.ToDateTime(dteBeginDate.Value);
                _travelGuideCashInfo.BeginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, int.Parse(ddlBeginHour.Value), int.Parse(ddlBeginMinute.Value), 00);
            }
            if (!String.IsNullOrEmpty(dteEndDate.Value))
            {
                var endDate = Convert.ToDateTime(dteEndDate.Value);
                _travelGuideCashInfo.EndDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, int.Parse(ddlEndHour.Value), int.Parse(ddlEndMinute.Value), 00);
            }
            
            _travelGuideCashInfo.Status = int.Parse(ddlStatus.SelectedValue);
            if (_travelGuideCashInfo.TravelGuideCashId == -1)
            {
                _travelGuideCashInfo.CreatedBy = ddlAdminUser.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlAdminUser.SelectedValue);
                _travelGuideCashInfo.CreatedDate = DateTime.Now;
                _travelGuideCashController.InsertTravelGuideCash(_travelGuideCashInfo);
            }
            else
            {
                _travelGuideCashInfo.UpdatedBy = UserId;
                _travelGuideCashInfo.UpdatedDate = DateTime.Now;
                _travelGuideCashController.UpdateTravelGuideCash(_travelGuideCashInfo);
            }
            Response.Redirect("/Pages/ListTravelGuideCash.aspx?xml=TravelGuideCash");
        }

        private void RenderForm()
        {
            ddlTravelGuide.SelectedValue = _travelGuideCashInfo.TravelGuideId.ToString();
            ddlTravelGuidePrice.SelectedValue = _travelGuideCashInfo.TravelGuidePriceId.ToString();
            dteBeginDate.Value = _travelGuideCashInfo.BeginDate.ToString("dd/MM/yyyy");
            ddlBeginHour.Value = _travelGuideCashInfo.BeginDate.Hour.ToString();
            ddlBeginMinute.Value = _travelGuideCashInfo.BeginDate.Minute.ToString();
            dteEndDate.Value = _travelGuideCashInfo.EndDate.ToString("dd/MM/yyyy");
            ddlEndHour.Value = _travelGuideCashInfo.EndDate.Hour.ToString();
            ddlEndMinute.Value = _travelGuideCashInfo.EndDate.Minute.ToString();
            ddlStatus.SelectedValue = _travelGuideCashInfo.Status.ToString();
            ddlAdminUser.SelectedValue = _travelGuideCashInfo.CreatedBy.ToString();
        }

        private void LoadTravelGuide()
        {
            ddlTravelGuide.DataSource = new TravelGuideController().ListTravelGuide()
                .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                .OrderBy(p => p.FullName);
            ddlTravelGuide.DataTextField = "FullName";
            ddlTravelGuide.DataValueField = "TravelGuideId";
            ddlTravelGuide.DataBind();

            ddlTravelGuide.Items.Insert(0, new ListItem("--- Chọn hướng dẫn viên ---", "-1"));
            ddlTravelGuidePrice.Items.Insert(0, new ListItem("--- Chọn yêu cầu (trình độ - ngôn ngữ - lịch trình) ---"));
        }

        private void LoadTime()
        {
            for (var i = 0; i < 24; i++)
            {
                var hour = i < 10 ? "0" + i : i.ToString();
                ddlBeginHour.Items.Add(new ListItem(hour, hour));
                ddlEndHour.Items.Add(new ListItem(hour, hour));
            }
            for (var i = 0; i < 60; i++)
            {
                var minute = i < 10 ? "0" + i : i.ToString();
                ddlBeginMinute.Items.Add(new ListItem(minute, minute));
                ddlEndMinute.Items.Add(new ListItem(minute, minute));
            }
        }
        private void LoadTravelGuidePrice()
        {
            ddlTravelGuidePrice.DataSource =
                new TravelGuidePriceController().ListTravelGuidePriceByTravelGuide(_travelGuideCashInfo.TravelGuideId)
                    .Where(p => p.Status == (int) Globals.DocumentStatus.Use)
                    .OrderBy(p => p.TravelGuidePriceName);
            ddlTravelGuidePrice.DataTextField = "TravelGuidePriceName";
            ddlTravelGuidePrice.DataValueField = "TravelGuidePriceId";
            ddlTravelGuidePrice.DataBind();

            ddlTravelGuidePrice.Items.Insert(0, new ListItem("--- Chọn yêu cầu (trình độ - ngôn ngữ - lịch trình) ---", "-1"));
        }
        private void LoadAdminUser()
        {
            ddlAdminUser.DataSource = new AdminUserController().ListAdminUser();
            ddlAdminUser.DataTextField = "Username";
            ddlAdminUser.DataValueField = "UserID";
            ddlAdminUser.DataBind();

            ddlAdminUser.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
    }
}